<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Users
 *
 * This model represents user authentication data. It operates the following tables:
 * - user account data,
 * - user profiles
 *
 * @package	Tank_auth
 * @author	Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class Report extends CI_Model
{
	private $report_table_name			= 'search_type_detail';			// report table
	private $web_info_table_name			= 'web_inf';			// report table
	private $report_db;
	function __construct()
	{
		parent::__construct();
		
		$ci =& get_instance();
		$this->report_table_name			= $ci->config->item('db_table_prefix', 'tank_auth').$this->report_table_name;
		$this->web_info_table_name			= $ci->config->item('db_table_prefix', 'tank_auth').$this->web_info_table_name;
	}

	/**
	 * Get overview by website
	 *
	 * @param	string
	 * @return	object
	 */
	function get_overview_by_website($website)
	{
		// load report database
		$this->report_db = $this->load->database('report',TRUE);
		
		/*
		 * select web info data from web inf
		 */
		$this->report_db->where('WEB_NAME', $website);
		$web_info_query = $this->report_db->get($this->web_info_table_name);
		$web_info_obj = $web_info_query->result();
		$get_payment_func = 'get_website_overview_rule_'.$web_info_obj[0]->RULE;
		
        /*
         * select report data from table search_type_detail
         */
		
		$this->report_db->select_sum('SEARCHES','impression');
		$this->report_db->select_sum('BID_CLICKS','click');
		
		if($web_info_obj[0]->RULE == 'a'){
			/*
			 * seperate money by PRODUCT SOURCE
			 */
			$this->report_db->select('WEB_SITE,MONTH(DATE) as month,YEAR(DATE) as year,PRODUCT_SOURCE as product_source');
			$this->report_db->group_by(array('YEAR(DATE)','MONTH(DATE)','PRODUCT_SOURCE'));	
		}
		else{
			$this->report_db->select('WEB_SITE,MONTH(DATE) as month,YEAR(DATE) as year');
			$this->report_db->group_by(array('YEAR(DATE)','MONTH(DATE)'));	
		} 
		
		$this->report_db->select_sum('EST_GROSS_REV','total_revenue');
		
		$this->report_db->where('WEB_SITE', $website);
		$report_query = $this->report_db->get($this->report_table_name);
		
		/*
		 * no data then return null
		 */
		if ($report_query->num_rows() <= 0) return NULL;
		
		$report_obj = $report_query->result();
		//echo "<pre>";print_r($report_obj);echo "</pre>";
		/*
		 * get website payment by month according rule
		 */
		$report_obj = $this->$get_payment_func($report_obj,$web_info_obj[0]);
		
		return $report_obj;
	}
	
	/**
	 * Get detail by website and date
	 *
	 * @param	string
	 * @param	string
	 * @param	string
	 * @return	object
	 */
	function get_detail_by_website_date($website,$start_date,$end_date,$chart_type)
	{
		// load report database
		$this->report_db = $this->load->database('report',TRUE);
		
		/*
		 * select web info data from web inf
		 */
		$this->report_db->where('WEB_NAME', $website);
		$web_info_query = $this->report_db->get($this->web_info_table_name);
		$web_info_obj = $web_info_query->result();
		$get_payment_func = 'get_website_overview_rule_'.$web_info_obj[0]->RULE;
		
        /*
         * select report data from table search_type_detail
         */
		
		$this->report_db->select_sum('SEARCHES','impression');
		$this->report_db->select_sum('BID_CLICKS','click');
		$this->report_db->where('DATE >=', $start_date);
		$this->report_db->where('DATE <=', $end_date);
		if($web_info_obj[0]->RULE == 'a'){
			/*
			 * seperate money by PRODUCT SOURCE
			 */
			$this->report_db->select('WEB_SITE,DATE as date,PRODUCT_SOURCE as product_source,MAX(SEARCHES) as max_impression,MIN(SEARCHES) as min_impression,MAX(BID_CLICKS) as max_click,MIN(BID_CLICKS) as min_click');
			$this->report_db->group_by(array('DATE','PRODUCT_SOURCE'));	
		}
		else{
			$this->report_db->select('WEB_SITE,DATE as date,MAX(SEARCHES) as max_impression,MIN(SEARCHES) as min_impression,MAX(BID_CLICKS) as max_click,MIN(BID_CLICKS) as min_click');
			$this->report_db->group_by('DATE');	
		} 
		
		$this->report_db->select_sum('EST_GROSS_REV','total_revenue');
		
		$this->report_db->where('WEB_SITE', $website);
		$this->report_db->where('DATE >=', $start_date);
		$this->report_db->where('DATE <=', $end_date);
		$report_query = $this->report_db->get($this->report_table_name);
		
		/*
		 * no data then return null
		 */
		if ($report_query->num_rows() <= 0) return NULL;
		
		$report_obj = $report_query->result();
		
		/*
		 * get website payment by month according rule
		 */
		$report_obj = $this->$get_payment_func($report_obj,$web_info_obj[0]);
		//echo "<pre>";print_r($report_obj);echo "</pre>";
		/*
		 * generate report chart json data
		 * default chart action is fetch click data
		 */
		$max_data = '';
		$min_data = '';
        if ($web_info_obj[0]->RULE != 'a') {
            $chart_data = array();
            foreach ($report_obj as $key=>$obj) {
                $chart_data[0][] = array($obj->date, $obj->$chart_type);//0 for click
                //$chart_data[1][] = array($obj->date, $obj->impression);//1 for impression
				if($max_data == ''){
					$max_data = $obj->$chart_type;
				}
				elseif($obj->$chart_type>$max_data){
					$max_data = $obj->$chart_type;
				}
				
				if($min_data == ''){
					$min_data = $obj->$chart_type;
				}
				elseif($obj->$chart_type<$min_data){
					$min_data = $obj->$chart_type;
				}
            }
        } else {
            $chart_data = array();
			$temp = array();
			foreach ($report_obj as $key=>$obj) {
				if(isset($temp[$obj->date])){
					$temp[$obj->date][$chart_type] += $obj->$chart_type;
					//$temp[$obj->date]['impression'] += $obj->impression;
				}
				else{
					$temp[$obj->date] = array();
					$temp[$obj->date][$chart_type] = $obj->$chart_type;
					//$temp[$obj->date]['impression'] = $obj->impression;
				}
				
				if($max_data == ''){
					$max_data = $temp[$obj->date][$chart_type];
				}
				elseif($temp[$obj->date][$chart_type]>$max_data){
					$max_data = $temp[$obj->date][$chart_type];
				}
				
				if($min_data == ''){
					$min_data = $temp[$obj->date][$chart_type];
				}
				elseif($temp[$obj->date][$chart_type]<$min_data){
					$min_data = $temp[$obj->date][$chart_type];
				}
            }
			
            foreach ($temp as $key=>$obj) {
                $chart_data[0][] = array($key, $obj[$chart_type]);//0 for click
                //$chart_data[1][] = array($key, $obj['impression']);//1 for impression
            }
        }
		
		$report_detail = array(
		'website_report_detail'=>$report_obj,
		'chart_data'=>$chart_data,
		'max_data'=>$max_data,
		'min_data'=>$min_data
		);
		
		return $report_detail;
	}
	
	/**
	 * Get website_overview rule a
	 * (by pm/cm clicks)
	 * @param	object
	 * @param	object
	 * @return	object (website payment)
	 */
	
	private function get_website_overview_rule_a($report_obj,$web_info_obj){
		foreach($report_obj as $key=>$val){
			if($report_obj[$key]->product_source == 'SS'){
				$report_obj[$key]->income = $report_obj[$key]->click * $web_info_obj->pm_click;
				$report_obj[$key]->product_source = 'PM';
			}
			else{
				$report_obj[$key]->income = $report_obj[$key]->click * $web_info_obj->cm_click;
			}
			
			$report_obj[$key]->ctr = ($report_obj[$key]->click / $report_obj[$key]->impression) * 100;
			if(!isset($report_obj[$key]->date)){
				$report_obj[$key]->date = $report_obj[$key]->year.'/'.$report_obj[$key]->month;
			}
			
		}
		return $report_obj;
	}
	
	/**
	 * Get website_overview rule b
	 * (by percent)
	 * @param	object
	 * @param	object
	 * @return	object (website payment)
	 */
	
	private function get_website_overview_rule_b($report_obj,$web_info_obj){
		foreach($report_obj as $key=>$val){
			$report_obj[$key]->income = $report_obj[$key]->total_revenue * 0.83 * ($web_info_obj->xpercent);
			$report_obj[$key]->ctr = @($report_obj[$key]->click / $report_obj[$key]->impression) * 100;
			if(!isset($report_obj[$key]->date)){
				$report_obj[$key]->date = $report_obj[$key]->year.'/'.$report_obj[$key]->month;	
			}	
		}
		return $report_obj;
	}

}

/* End of file users.php */
/* Location: ./application/models/auth/users.php */